/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using DaveSexton.DocProject.Resources;

namespace DaveSexton.DocProject.Engine
{
	/// <summary>
	/// Exception that is thrown when an external process returns a non-zero exit code.
	/// </summary>
	[global::System.Serializable]
	public class ExternalProcessException : Exception
	{
		public int ExitCode
		{
			get
			{
				return exitCode;
			}
		}

		private readonly int exitCode;

		public ExternalProcessException() { }
		public ExternalProcessException(string message) : base(message) { }
		public ExternalProcessException(string message, Exception inner) : base(message, inner) { }

		public ExternalProcessException(string processName, int exitCode)
			: base(GetErrorMessage(processName, exitCode))
		{
			this.exitCode = exitCode;
		}

		public ExternalProcessException(string processName, int exitCode, Exception inner)
			: base(GetErrorMessage(processName, exitCode), inner)
		{
			this.exitCode = exitCode;
		}

		protected ExternalProcessException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
			if (info != null)
				exitCode = info.GetInt32("exitCode");
		}

		[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.LinkDemand, SerializationFormatter = true)]
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			if (info != null)
				info.AddValue("exitCode", exitCode);

			base.GetObjectData(info, context);
		}

		public static string GetErrorMessage(string processName, int exitCode)
		{
			return string.Format(System.Globalization.CultureInfo.CurrentCulture, Errors.ExternalProcessExceptionMessage, processName, exitCode);
		}
	}
}
